package ${pkgName};

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fire.common.entity.FireMsg;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.fire.common.util.WebConstant;
import com.fire.common.util.UUIDUtils;
import com.github.pagehelper.PageInfo;
import com.fire.code.user.entity.UserEntity;

<#list import_list as imports>
import ${imports};
</#list>

@Service
@Transactional
public class ${className} implements ${serviceClassName} {

	@Autowired
	private ${daoImplClassName} ${daoImplVisitName};
	
	/**
	 * @Title: list
	 * @author: close
	 * @Description: 获得grid所需的list
	 * @param  ${modebeanClassName}
	 * @return List<${modebeanClassName}>
	 * @throws Exception
	 */
	@Override
	public List<${modebeanClassName}> list(${modebeanClassName} entity) throws Exception {
		entity.setDel_flag(WebConstant.UN_DEL);	
		return ${daoImplVisitName}.list(entity);
	}
	
	/**
	 * @Title: listWithPage
	 * @author: close
	 * @Description: 获得grid所需的list
	 * @param page		当前页数
	 * @param rows	每页条数
	 * @param ${modebeanClassName} 查询实体类
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> listWithPage(int page,int rows,${modebeanClassName} entity) throws Exception {
		
		// 获取第1页，10条内容，默认查询总数count
		if(page!=0 && rows!=0)		
		PageHelper.startPage(page, rows);

		List<${modebeanClassName}> list = this.list(entity);

		PageInfo<${modebeanClassName}> pageInfo = new PageInfo<${modebeanClassName}>(list);

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(WebConstant.ROWS, pageInfo.getList());
		resultMap.put(WebConstant.TOTAL, pageInfo.getTotal());

		return resultMap;
	}

	/**
	 * @Title: search
	 * @author: close
	 * @Description: 获得需修改的数据
	 * @param ${table.pks[0].column_name}	主键
	 * @return ${modebeanClassName}
	 * @throws Exception
	 */
	@Override
	public ${modebeanClassName} search(String ${table.pks[0].column_name}) throws Exception {
		return this.${daoImplVisitName}.getById(${table.pks[0].column_name});
	}

	/**
	 * @Title: save
	 * @author: close
	 * @Description: 添加或修改通用方法
	 * @param ${modebeanClassName}	需要持久化操作的实体
	 * @param optName add/edit
	 * @return FireMsg<${modebeanClassName}>
	 * @throws Exception
	 */
	@Override
	public FireMsg<${modebeanClassName}> save(${modebeanClassName} entity,String optName,UserEntity user) throws Exception {
		FireMsg<${modebeanClassName}> msg = new FireMsg<${modebeanClassName}>();
		if ("add".equals(optName)) {
			entity.set${table.pks[0].column_name?cap_first}(UUIDUtils.get32());
			this.${daoImplVisitName}.insert(entity);
		} else if ("edit".equals(optName)) {
			this.${daoImplVisitName}.update(entity);
		} else {
			msg.setFlag(false);
			msg.setMsg("optName标识无法识别");
			return msg;
		}
		msg.setFlag(true);
		return msg;		
	}

	/**
	 * @Title: delete
	 * @author: close
	 * @Description: 删除
	 * @param ${table.pks[0].column_name} 主键
	 * @return FireMsg<${modebeanClassName}> 删除结果
	 * @throws Exception
	 */
	@Override
	public FireMsg<${modebeanClassName}> delete(List<String> ${table.pks[0].column_name}s) throws Exception {
		FireMsg<${modebeanClassName}> msg = new FireMsg<${modebeanClassName}>();
		int[] flag =  this.${daoImplVisitName}.delete(${table.pks[0].column_name}s);
		for(int i:flag){
			if (0 == i) {
				msg.setFlag(false);
			}		
		}
		return msg;
	}
	
}
